<!-- mod by luoji  all file -->
<template>
  <a-modal
    v-model="visible"
    :title="'发起订单'"
    :maskClosable="false"
    :keyboard="false"
    :confirmLoading="confirmLoading"
    :destroyOnClose="true"
    :width="700"
    @ok="ok"
    @cancel="close"
  > 
    <div class="popWindow" v-if="isRouterAlive||true">
      <a-row>
        <a-col :span="4">
          截止日期
        </a-col>
        <a-col :span="17">
          <a-date-picker v-model="date"/>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="4">
          供应商
        </a-col>
        <a-col :span="20">
          <staff-selector
            v-model="user"
            :options="taffSelectorOpts"
          >
          </staff-selector>
        </a-col>
      </a-row>
    </div>
  </a-modal>
</template>

<script lang="ts">

import {
  Component, Vue, Prop, Watch
} from 'vue-property-decorator';
import Axios from 'axios'
import {
  Modal,
  Row,
  Col,
  Input,
  Select,
  Alert
} from '@h3/antd-vue';

import StaffSelector from "@cloudpivot/form/src/renderer/components/shared/staff-selector.vue";

import { homeApi,userApi } from "@cloudpivot/api";

@Component({
  name: 'form-modify-start-order',
  components: {
    AModal: Modal,
    ARow: Row,
    ACol: Col,
    Alert,
    ATextarea: Input.TextArea,
    ASelect: Select,
    ASelectOption: Select.Option,
    StaffSelector
  }
})
export default class FormStartOrderModal extends Vue {
  
  user1: any = null;
  user: any = null;
  date: any = null;
  visible: boolean = false;

  confirmLoading: boolean = false;
  action: any = "";
  isRouterAlive: boolean = true;
  layout: any = {
    left: 6,
    right: 18
  }

  taffSelectorOpts: any = {
    selectOrg: false,
    selectUser: true,
    showModel: true,
    mulpitle: false,
    showSelect: true,
    placeholder: '请选择',
    title: '选择供应商',
    rootNode: [{ id : '8aeec3a67ae6cf45017aeff3dcf915d6'}]
  }

  async init(num: number, action: any) {
    this.action = action;
    var tmp;
    let user: any = window.sessionStorage.getItem('user');

      if (user) {

        user = JSON.parse(user);

      } else {

        user = { id:'',name:'' };

      }
      
      // if(user.id != '' && user.id != null && user.id.length > 0){
      //   tmp = [{
      //       type: 1, //1部门，3人员
      //       imgUrl: '//www.baidu.com/img1.png',
      //       name:'',
      //       id: user.id,
      //       departmentId : '' ,
      //       departments : [] 
      //     }];
      //   var userinfo :any = await userApi.getUserInfo(user.id);
      //   if(userinfo && userinfo.errcode === 0) { 
      //     tmp[0].departmentId = userinfo.data.departmentId;
      //     tmp[0].name = userinfo.data.name;
      //     this.reload();
      //   }
      // }
      this.user = tmp;
      this.user1=tmp;
      var t = new Date();
      this.date = t.getFullYear() + '-' + (t.getMonth()+1) + '-' + t.getDate();
    this.show();
  }
  show() {
    this.visible = true;
  }

  close() {
    this.confirmLoading = false;
    this.visible = false;
  }

  ok() {
    var idsStr=sessionStorage.getItem("sessionKey");
    var sessionCode= sessionStorage.getItem("sessionCode");
    let arr: any = null;
    if(idsStr){
        arr = idsStr.split(',');
    }
    var suppliers:any=[];
    var newparams :any =new Object();
    newparams.id=this.user[0].id; //供应商id
    suppliers.push(newparams);
    var ajaxurl="";
    switch(sessionCode)
    {
      case "CYCLWPXQC":
        ajaxurl="/api/materials/addMaterials";
        break;
      case "BPCG":
        ajaxurl="/api/invitation/addMaterials";
        break;
      case "DZBGXQC":
        ajaxurl="/api/bigAnt/addMaterials";
        break;
    }

    Axios.post(ajaxurl,{
      ids: arr,
      suppliers: suppliers,
      }).then((res:any)=>{
        debugger;
        if(res.errmsg!="SUCCESS"){
          alert("订单信息生成失败");
        }else{
          this.close();
        }
      });

    console.log(sessionStorage.getItem("sessionKey"));
  }

  async getUserInfo(userId: string, item: any){
    const res: any = await userApi.getUserInfo(userId);
    if(res && res.errcode === 0) {
      item.b = res.data.name;
      item.c = res.data.mobile;
      this.reload();
    }else{
      this.$message.error(res.errmsg);
    }
  }

  reload(){
      this.isRouterAlive=false;
      this.$nextTick(function(){
        this.isRouterAlive=true
      })
    }
}
</script>

<style lang="less" scoped>
    .popWindow .ant-col{
      padding: 2px !important;
    }

    .popWindow .popHeader{
      margin-top: 10px;
      padding: 5px 8px;
      border-top: 1px solid #DDD;
    }

    .popWindow .popWindow_row{
      border-bottom: 1px solid #DDD;
    }
</style>
